<?php
require_once 'SurveysCompleted.php';

class Survey_ChartController extends Zend_Controller_Action
{
    public $contexts = array('browse' => array('xml'));

    function init()
    {
        $this->getHelper('layout')->setLayout('json');
    }
    
    public function hitsAction()
    {
        $sid = $this->_request->getParam('SurveyId');
        $qid = $this->_request->getParam('QuestionId');
        $hits = SurveysCompleted::hitsByCycle($qid,$sid);  

        $data = array();
        foreach ($hits as $hit) $data[$hit->AnswerId][$hit->Cycle] = intval($hit->Hits);
        
        /*
         * fix cycles with incompleted series by
         */
        $xlabel = array();
        foreach($data as $cdata)
            foreach($cdata as $i => $j)
                $xlabel[$i] = true;
        foreach($data as $key => $cdata)
            foreach($xlabel as $i => $j)
                if(!isset($cdata[$i]))
                    $data[$key][$i] = 0;
        
        $chart = $this->_helper->chart->bar($data,$this->view->translate('Cycle'),$this->view->translate('Answers')); 
        //foreach ($hits as $hit) $data[$hit->Cycle][$hit->AnswerId] = intval($hit->Hits); 
        //$chart = $this->_helper->chart->barStack($data,$this->view->translate('Cycle'),$this->view->translate('Answers')); 
                
        echo $chart->toPrettyString(); 
        exit;                
    }
}
